package engine;

import java.awt.Graphics2D;

import java.awt.List;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.background.ImageBackground;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.AnimatedSprite;
import com.golden.gamedev.object.Timer;
import com.golden.gamedev.object.collision.BasicCollisionGroup;

import sprites.AirBubble;
import sprites.BubblefishBobSprite;
import sprites.Bubble;
import sprites.PathBubble;
import sprites.Bob;

/**
 * This class will be used to merge ProgBob and BubblefishBob. It will
 * eventually be renamed BubblefishBob, and it will contain the code for the
 * game (in a much better way).
 */
public class BubblefishBobGame extends GameObject
{

	/*
     * The field holds all game objects.
     */
	
	private String myString;
	
    private PlayField field;

    private Background bg;

    private Random rng;
	private int myLevel;
	
	private SpriteGroup BOB, BUBBLEFIRED, BUBBLEPATH, BONUSES, ITEMS, EVILFISH;
	    
	 /*
     * Bob is the fish that the player controls. BubbleFired are the bubbles
     * that Bob fires; BubblePath are the bubbles that are floating on the
     * path (can have fish in them or not). Bonuses are powerups that drop down
     * to Bob. Items are special weapons that Bob can use, like torpedos and
     * small rocks. Finally, EvilFish is the evil fish that eats bubbles.
     */
	private BubblefishBobSprite bob;
	    
	private PathBubble pathbubble;
	private ArrayList<Point> myPoints;

	    /*
	     * The fire timer controls the firing rate of the player. The spawn timer
	     * controls how fast new bubbles spawn on the path.
	     */
	private Timer fireTimer, spawnTimer;
	
    public BubblefishBobGame(GameEngine parent, int level) {
		super(parent);
		myLevel = level;
		myString = "level" + level;
		myPoints = new ArrayList<Point>();
	}
    
   
    @Override
    public void initResources()
    {
        rng = new Random();

        
        bg = new ImageBackground(getImage("resources/images/seagrass.jpg"));
        field = new PlayField(bg);

        BOB = field.addGroup(new SpriteGroup("Bob"));
        BUBBLEFIRED = field.addGroup(new SpriteGroup("Bubbles fired"));
        BUBBLEPATH = field.addGroup(new SpriteGroup("Bubbles on path"));
        BONUSES = field.addGroup(new SpriteGroup("Bonuses"));
        ITEMS = field.addGroup(new SpriteGroup("Items"));
        EVILFISH = field.addGroup(new SpriteGroup("Evil Fish"));
        Level l = new Level(myString);
        myPoints = l.getPoints();
        for(int i = 0; i<myPoints.size()-1; i++){
        	pathbubble = new PathBubble(this, myPoints.get(i).getX(), myPoints.get(i).getY());
        	BUBBLEPATH.add(pathbubble);
        }
        /*
         * Add Bob to the screen and make him belong to the BOB group.
         */
        bob = new Bob(this, 200, 200);
        BOB.add(bob);
    }

    @Override
    public void update(long elapsedTime)
    {
        field.update(elapsedTime);

        /*
         * Exit if the player hits escape.
         */
        if(keyDown(KeyEvent.VK_ESCAPE)) finish();
        
        /*
         * DEBUG: clicking lets you go on to the next level
         */
        if(click())
        {
        	parent.nextGameID=2;
        	finish();
        }
    }

    @Override
    public void render(Graphics2D g)
    {
        field.render(g);
    }

}
